package com.bawei.weekone2207a.view

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.weekone2207a.R
import com.bawei.weekone2207a.view.nav.HomePage
import com.bawei.weekone2207a.view.nav.MinePage
import com.bawei.weekone2207a.view.nav.ShopCardPage

/**
 * 作者：赵亮
 * 时间：2024/12/14 9:44
 * 功能：主页面
 */

// 底部导航数据类
data class BottomEntity(val img: Int, val text: String)

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainPage(navController: NavController = rememberNavController()) {

    val list = listOf(
        BottomEntity(R.drawable.icon1275, "首页"),
        BottomEntity(R.drawable.icon64, "购物车"),
        BottomEntity(R.drawable.icon1405, "我的")
    )

    // 底部导航选中
    var selectedIndex by remember {
        mutableStateOf(0)
    }

    Scaffold(
        bottomBar = {
            NavigationBar {
                list.forEachIndexed { index, it ->
                    NavigationBarItem(
                        selected = selectedIndex == index,
                        onClick = { selectedIndex = index },
                        label = { Text(text = it.text)},
                        icon = { Image(painter = painterResource(id = it.img), contentDescription = null ) })
                }
            }
        }
    ) {
        when(selectedIndex) {
            0 -> HomePage(navController)
            1 -> ShopCardPage()
            2 -> MinePage()
        }
    }
}

@Preview
@Composable
fun MainPagePreview() {
    MainPage()
}

